package ru.spbau.database.group3.queryEvaluator;

import ru.spbau.database.group3.engine.Column;
import ru.spbau.database.group3.filesAndAccess.Record;
import ru.spbau.database.group3.queryEvaluator.QueryResult;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: stas
 * @date: 2/11/13
 * @time: 6:07 PM
 */
public class SelectResult implements QueryResult {

    public SelectResult(Collection<Record> records, List<Column> columns) throws IOException {
        this.result = new ArrayList<String>();
        String head = "";
        for(Column c : columns) {
            head += c.getName();
            for(int i = 0; i < c.getType().size() / 2 - c.getName().length(); ++i) {
                head += " ";
            }
            head += "; ";
        }
        result.add(head);
        for(Record r : records) {
            String str = "";
            for(Object value : r.getColumnsValue(columns)) {
                str += value.toString() + "; ";
            }
            result.add(str);
        }
    }

    @Override
    public String getStringResult() {
        String res = "";
        for(String s : result) {
            res += s + "\n";
        }
        return res;
    }

    private final List<String> result;
}
